﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using SWCInvoice.Model;

namespace SWCInvoice.Utils
{
    /// <summary>
    /// A load filter helper class
    /// </summary>
    public class FilterHandler
    {
        /// <summary>
        /// Load all Filters from a given xml file
        /// </summary>
        /// <param name="fullFileName">The fullfilename of the xml file</param>
        /// <returns>A identifier collection</returns>
        public static Identifier.IdentifierCollection GetFilter(string fullFileName)
        {
            string name = null;
            string group = null;
            string searchExpression = null;
            string searchKeyword = null;
            string expression = null;
            string inheritance = null;
            string additional = null;
            string description = null;

            Identifier.IdentifierCollection identifiers = new Identifier.IdentifierCollection();

            XmlDocument xmlDoc=null;

            XmlNodeList nodeListSection = null;
            try
            {
                xmlDoc = new XmlDocument();
                xmlDoc.Load(fullFileName);

                XmlNode root = xmlDoc.DocumentElement;

                nodeListSection = root.SelectNodes("Filters/Filter");

                foreach (XmlNode item in nodeListSection)
                {
                    name = null;
                    group = null;
                    searchExpression = null;
                    searchKeyword = null;
                    expression = null;
                    inheritance = null;
                    additional = null;
                    description = null;

                    try { name = item.Attributes["name"].Value; }
                    catch { }

                    try { group = item.SelectSingleNode("Group").InnerText.Trim(); }
                    catch { }

                    try { searchExpression = item.SelectSingleNode("SearchExpression").InnerText.Trim(); }
                    catch { }

                    try { searchKeyword = item.SelectSingleNode("SearchKeyword").InnerText.Trim(); }
                    catch { }

                    try { expression = item.SelectSingleNode("Expression").InnerText.Trim(); }
                    catch { }

                    try { inheritance = item.SelectSingleNode("Inheritance").InnerText.Trim(); }
                    catch { }

                    try { additional = item.SelectSingleNode("Additional").InnerText.Trim(); } 
                    catch { }

                    try { description = item.SelectSingleNode("Description").InnerText.Trim(); }
                    catch { }

                    identifiers.Add(Identifier.Create(name, group, searchExpression, searchKeyword, expression, inheritance, additional, description));
                }
             }
             catch (Exception ex) { }

             return identifiers;
        
        }
    }
}
